"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   :主备配置文件的双机连接字符串为空
Create At   : 2020/12/07
@zou_jialiang0505328126
Description :
    1、 修改主机中配置文件中双机连接字符串为空
    2、 重启数据库服务
    3、 恢复主机中配置文件并重启
    4、 修改备机中配置文件中双机连接字符串
    5、 重启数据库服务
Expect      :
    1.修改成功
    2.stop成功，start失败，双机关系无法建立
    3.执行成功，重启成功
    4.修改成功
    5.stop成功，start失败，双机关系无法建立
History     :
"""

import unittest
import os
from yat.test import macro
from yat.test import Node
import sys
sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ModifyAudit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta1 = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case118.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.dbPrimaryRoot = Node(node='PrimaryRoot')
        self.dbStandby1Root = Node(node='Standby1Root')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.primary_ture_rep = ""
        self.standby_ture_rep = ""
        cmd = 'cat /etc/hostname'
        self.primary_name = self.dbPrimaryRoot.sh(cmd).result()
        self.standby_name = self.dbStandby1Root.sh(cmd).result()

    def test_basebackup(self):
        self.log.info('=============主机获取配置参数replconninfo1============')
        get_rep_cmd = f"cat {os.path.join(macro.DB_INSTANCE_PATH, 'postgresql.conf')} | grep replconninfo1 "
        self.log.info(get_rep_cmd)
        rep_msg = self.dbPrimaryDbUser.sh(get_rep_cmd).result()
        self.log.info(rep_msg)
        self.assertIsNotNone(rep_msg)
        self.primary_ture_rep = rep_msg.split("'")[1].strip()
        self.log.info(self.primary_ture_rep)

        self.log.info('------------------------1、 修改主机中配置文件中双机连接字符串为空-------------------------')
        result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, "replconninfo1 =''", self.primary_name)
        self.assertTrue(result)

        self.log.info('---------------------------2、 重启数据库服务------------------------')
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertFalse(result)

        self.log.info('------------------------------3、 恢复主机中配置文件并重启--------------------------')
        result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 ='{self.primary_ture_rep}'",
                                               self.primary_name)
        self.assertTrue(result)

        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        self.log.info('=============备机获取配置参数replconninfo1============')
        get_rep_cmd = f"cat {os.path.join(macro.DB_INSTANCE_PATH, 'postgresql.conf')} | grep replconninfo1 "
        self.log.info(get_rep_cmd)
        rep_msg = self.dbStandby1DbUser.sh(get_rep_cmd).result()
        self.log.info(rep_msg)
        self.assertIsNotNone(rep_msg)
        self.standby_ture_rep = rep_msg.split("'")[1].strip()
        self.log.info(self.standby_ture_rep)
        self.log.info('-----------------------------4、 修改备机中配置文件中双机连接字符串------------------------')
        result = self.commonshsta1.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, "replconninfo1 =''", self.standby_name)
        self.assertTrue(result)

        self.log.info('---------------------------5、 重启数据库服务------------------------')
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertFalse(result)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        result = self.commonshsta1.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 ='{self.standby_ture_rep}'", self.standby_name)
        self.log.info(result)
        result = self.commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 ='{self.primary_ture_rep}'", self.primary_name)
        self.log.info(result)
        self.commonshpri.stopDbCluster()
        self.commonshpri.startDbCluster()

        self.log.info('----------Opengauss_Reliability_Dbsys_Case118.py 执行结束-----------------------')
